#!/bin/zsh

# change your shell to zsh first

readonly SCRIPT_DIR=$(dirname "$(realpath "$0")")
readonly USER="aiduorin"
readonly USER_HOME="/home/aiduorin"
readonly GIT_USER="aiduorin"
readonly GIT_EMAIL="aiduorin@outlook.com"

function install_pkg {
  pacman -S --noconfirm "$1" || return 1
}

function in_temp_dir {
  if [ -z "$1" ]; then return 1; fi
  local pre tmp func params
  pre="$(pwd)"
  tmp="$(mktemp -d)"
  func="$1"
  shift
  params=("$@")
  chmod 777 "${tmp}"
  cd "${tmp}" || return 1
  $func "${params[@]}"
  local exit_status="$?"
  cd "${pre}" || return 1
  rm -rf "${tmp}"
  return "${exit_status}"
}

function install_aur {
  if [ -z "$1" ]; then return 1; fi
  git clone "$1" || return 1
  local repo
  repo="$(basename "$1" .git)"
  chmod 777 "./${repo}"
  cd "${repo}" || return 1
  sudo -u "${USER}" zsh -c "makepkg -si" || return 1
}

function run_config {
  if [ -z "$1" ]; then return 1; fi
  local config_func="$1"
  printf "\n------------%s------------\n" "${config_func}"
  if $config_func; then
    printf "\n------------%s OK------------\n" "${config_func}"
  else
    printf "\n------------%s FAILED------------\n" "${config_func}"
    return 1
  fi
}

function config_mirror {
  install_pkg "reflector" || return 1
  reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist || return 1
  pacman -Syu --noconfirm || return 1
}

function config_ssh {
  install_pkg "openssh less" || return 1
  sudo -u "${USER}" zsh -c "ssh-keygen -t rsa -C ${GIT_EMAIL}" || return 1
  cat "${SCRIPT_DIR}/lib/ssh-agent.sh" >>"${USER_HOME}/.zshrc" || return 1
  printf "\nalias sshadd='ssh-add ~/.ssh/id_rsa'\n" >>"${USER_HOME}/.zshrc" || return 1
}

function config_git {
  install_pkg "git lazygit" || return 1
  printf "\nalias lg='lazygit'\n" >>"${USER_HOME}/.zshrc" || return 1
  sudo -u "${USER}" zsh -c "git config --global user.name ${GIT_USER}" || return 1
  sudo -u "${USER}" zsh -c "git config --global user.email ${GIT_EMAIL}" || return 1
  cat "${SCRIPT_DIR}/lib/github-ssh.conf" >>"${USER_HOME}/.ssh/config" || return 1
}

function config_yay {
  install_pkg "base-devel" || return 1
  in_temp_dir install_aur "https://aur.archlinux.org/yay.git" || return 1
}

function config_font {
  install_pkg "noto-fonts noto-fonts-cjk unzip" || return 1
  # shellcheck disable=SC2317
  function install_geist_mono {
    curl -L -o GeistMono.zip https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/GeistMono.zip || return 1
    test -e ./GeistMono.zip || return 1
    mkdir GeistMono
    unzip ./GeistMono.zip -d ./GeistMono
    mv ./GeistMono /usr/share/fonts/
  }
  in_temp_dir install_geist_mono || return 1
  mkdir -p "${USER_HOME}/.config/fontconfig/" && cat "${SCRIPT_DIR}/lib/fonts.conf" >"${USER_HOME}/.config/fontconfig/fonts.conf" || return 1
}

function config_fnm {
  export SHELL="/bin/zsh"
  sudo -u "${USER}" zsh -c "curl -fsSL https://fnm.vercel.app/install | bash" || return 1
}

function config_starship {
  install_pkg "starship" || return 1
  printf '\neval "$(starship init zsh)"\n' >>"${USER_HOME}/.zshrc"
  cp -f "${SCRIPT_DIR}/lib/starship.toml" "${USER_HOME}/.config/starship.toml" || return 1
}

function config_bluetooth {
  install_pkg "bluez bluez-utils" || return 1
  modprobe btusb || return 1
  systemctl enable bluetooth.service && systemctl start bluetooth.service
}

function config_input_method {
  sudo -u "${USER}" zsh -c "yay -S fcitx5-pinyin-moegirl"
  install_pkg "fcitx5-im fcitx5-chinese-addons fcitx5-qt fcitx5-gtk fcitx5-pinyin-zhwiki" || return 1
}

function config_grub_theme {
  # shellcheck disable=SC2317
  function install_grub_theme {
    tar -xvf "${SCRIPT_DIR}/lib/grub-theme.tar.xz" || return 1
    cd "./grub-theme" || return 1
    ./install.sh || return 1
  }
  in_temp_dir install_grub_theme || return 1
}


function config_vim {
  sudo -u "${USER}" zsh -c "yay -S zsh-vi-mode" || return 1
  {
    printf '\ninoremap jk <Esc>'
    printf '\ninoremap kj <Esc>'
    printf '\nset nu rnu'
    printf '\nlet &t_SI = "\\e[5 q"'
    printf '\nlet &t_EI = "\\e[1 q"'
  } >>/etc/vimrc || return 1
  {
    printf '\nZVM_VI_INSERT_ESCAPE_BINDKEY=jk'
    printf '\nsource /usr/share/zsh/plugins/zsh-vi-mode/zsh-vi-mode.plugin.zsh'
  } >>"${USER_HOME}/.zshrc" || return 1
}

function config_docker {
  install_pkg "docker docker-compose" || return 1
  systemctl enable docker.socket && systemctl start docker.socket
  usermod -aG docker "${USER}"
}

function config_common {
  install_pkg "telegram-desktop okular flameshot gwenview kamoso dragon dbeaver"
  sudo -u "${USER}" zsh -c "yay -S google-chrome"
  sudo -u "${USER}" zsh -c "yay -S windsurf"
  printf '\nalias wsf="windsurf . > /dev/null 2>&1 &"' >> "${USER_HOME}/.zshrc"
}

function config_eza {
  install_pkg "eza"
  echo -e "\nalias el=\"eza -al --icons --git --git-repos --group-directories-first --time-style '+%y/%m/%d'\"\n" >>"${USER_HOME}/.zshrc" || return 1
}

run_config config_mirror || exit 1
run_config config_ssh
run_config config_git
run_config config_yay
run_config config_font
run_config config_fnm
run_config config_starship
run_config config_bluetooth
run_config config_input_method
run_config config_grub_theme
run_config config_vim
run_config config_docker
run_config config_common
run_config config_eza
